var Md5 = require('../../utils/md5.js');
var timeSecond = Math.floor(Date.now() * 0.001);  //获取时间戳: 
var SignInfoData = Md5.hexMD5(timeSecond + 'nativetalk')//md5加密;
var WxParse = require('../../wxParse/wxParse.js');
const app = getApp();
var httpNativetalk = app.globalData.httpNativetalk;
var httpWeike = app.globalData.httpWeike;
Page({
  data: {
    navBar: ['待上课', '可取消', '已完成'],
    NavIdx: 0,
    imgUrl: 'http://file.waijiaojun.com/waijiaojun/icon',
    cancelRuleFlag: true,
  },
  onShow: function () {
    var that = this;
    this.unfinishData();
    this.finishData();
    //取消规则数据请求
    wx.request({
      url: httpNativetalk + '/app/11/get/cancel/rule',
      data: {
      },
      header: {
        "Timestamp": timeSecond,
        "SignInfo": SignInfoData,
        "content-Type": "application/json",
        "client_type": "3",
        "version": 15,

      },
      method: 'POST',
      success: function (res) {
        console.log(res)
        that.setData({
          cancelContentText: res.data.obj.cancelRule
        })
      }

    })
  },
  //待上课,可取消数据的请求
  unfinishData: function () {
    var that = this;
    wx.request({
      url: httpNativetalk + '/app/7/token/teacher/reserve/list/unfinish',
      data: {
      },
      header: {
        "Timestamp": timeSecond,
        "SignInfo": SignInfoData,
        "content-Type": "application/json",
        "client_type": "3",
        "version": 15,
        'token': app.globalData.token,
      },
      method: 'POST',
      success: function (res) {
        console.log(res)
        if (res.data.obj !== null) {
          var reserveListData = res.data.obj.reserveList;
          function getDate(time) {
            var date = new Date(time);
            var Y = date.getFullYear() + '-';
            var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
            var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
            var H = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
            var Min = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
            return (M + D + ' ' + H + Min);
          };
          for (var i = 0; i < reserveListData.length; i++) {
            var cancleTime = reserveListData[i].reserve_start_time;
            reserveListData[i].reserve_start_time = getDate(reserveListData[i].reserve_start_time);
            reserveListData[i].head_portrait = 'http://file.waijiaojun.com/' + reserveListData[i].head_portrait;
          }
          that.setData({
            cancleTime: cancleTime,
            unfinishListData: reserveListData,
          })

        } else {
          //  djflasdjflajsdfklajsdflkjasldkfj
          that.setData({
            appointFlag: true,
          })
        }

      },
      fail: function (res) { },
      complete: function (res) { },
    })

  },
  //已完成数据请求
  finishData: function () {
    var that = this;
    wx.request({
      url: httpNativetalk + '/app/7/token/teacher/reserve/list/finish',
      data: {
      },
      header: {
        "Timestamp": timeSecond,
        "SignInfo": SignInfoData,
        "content-Type": "application/json",
        "client_type": "3",
        "version": 15,
        'token': app.globalData.token,
      },
      method: 'POST',
      success: function (res) {
        console.log(res)
        if (res.data.obj !== null) {
          var reserveListData = res.data.obj.reserveList;
          function getDate(time) {
            var date = new Date(time);
            var Y = date.getFullYear() + '-';
            var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
            var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
            var H = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
            var Min = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
            return (M + D + ' ' + H + Min);
          };
          for (var i = 0; i < reserveListData.length; i++) {
            reserveListData[i].reserve_start_time = getDate(reserveListData[i].reserve_start_time);
            reserveListData[i].head_portrait = 'http://file.waijiaojun.com/' + reserveListData[i].head_portrait;
          }
          that.setData({
            finishListData: reserveListData,
          })
        } else {
          // /djflasdjflajsdfklajsdflkjasldkfj
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  //小导航函数
  navFun: function (e) {
    this.setData({
      NavIdx: e.currentTarget.dataset.idx,
    })
  },
  //取消规则按钮函数
  cancelRuleFun: function (e) {
    this.setData({
      cancelRuleFlag: false,
    })
  },
  //关闭取消规则提示
  closeCancelRuleFun: function (e) {
    this.setData({
      cancelRuleFlag: true,
    })
  },
  //取消预约按钮函数
  cancleAppoinFun: function (e) {
    console.log(e)
    var that = this;
    var reserve_id = e.currentTarget.dataset.reserve_id;
    var cancle_time = e.currentTarget.dataset.cancle_time;
    var date = Date.parse(new Date());
    var times = cancle_time - date ;
    if (times < 14400000 && times>3600000) {
      console.log("距离课程开始4小时内" + times)
      wx.showModal({
        title: '温馨提示',
        content: '四个小时以内取消扣除账户20元',
        success: function (res) {
          if (res.confirm) {
            // console.log('用户点击确定');
            wx.request({
              url: httpNativetalk + '/app/7/token/teacher/reserve/cancel',
              data: {
                "reserve_id": reserve_id,
              },
              header: {
                "Timestamp": timeSecond,
                "SignInfo": SignInfoData,
                "content-Type": "application/json",
                "client_type": "3",
                "version": 15,
                'token': app.globalData.token,
              },
              method: 'POST',
              success: function (res) {
                console.log(res);
                if (res.data.mark == 0) {
                  wx.showToast({
                    title: '取消成功',
                    icon: 'success',
                    duration: 2000,
                    success: function () {
                      that.unfinishData();
                      that.finishData();
                    }
                  });
                }
              }
            })
          } else if (res.cancel) {
            // console.log('用户点击取消')
          }
        }
      })
    }
    if (times > 14400000 || times < 3600000){
      console.log("距离课程开始4小时外" + times)
      wx.request({
        url: httpNativetalk + '/app/7/token/teacher/reserve/cancel',
        data: {
          "reserve_id": reserve_id,
        },
        header: {
          "Timestamp": timeSecond,
          "SignInfo": SignInfoData,
          "content-Type": "application/json",
          "client_type": "3",
          "version": 15,
          'token': app.globalData.token,
        },
        method: 'POST',
        success: function (res) {
          console.log(res);
          if (res.data.mark == 0) {
            wx.showToast({
              title: '取消成功',
              icon: 'success',
              duration: 2000,
              success: function () {
                that.unfinishData();
                that.finishData();
              }
            });
          }
          if (res.data.mark == 1) {
            wx.showModal({
              title: '温馨提示',
              content: res.data.tip,
              success: function (res) {
                if (res.confirm) {
                  // console.log('用户点击确定')
                } else if (res.cancel) {
                  // console.log('用户点击取消')
                }
              }
            })
          }
        }
      })
    }
  }
})